<template>
    <div class="app-container">
        <div class="ces-main">



            <el-form ref="form" :model="FormData" :inline="true" :rules="rules" label-position="right"
                label-width="120px">


                <el-form-item v-if="this.StepSN == 0" style="height:5px">

                    <el-button type="primary" size="mini" icon="el-icon-tickets" @click="confirmData(0)">保存</el-button>
                </el-form-item>
                <el-form-item v-if="this.StepSN == 0" style="height:5px">
                    <el-button type="primary" size="mini" icon="el-icon-check" @click="confirmData(1)">提交</el-button>
                </el-form-item>

                <el-form-item v-if="this.StepSN > 0" style="height:5px">
                    <el-button type="primary" size="mini" icon="el-icon-tickets" @click="confirmData(0)">保存</el-button>
                </el-form-item>
                <el-form-item v-if="this.StepSN > 0" style="height:5px">
                    <el-button type="primary" size="mini" icon="el-icon-check" @click="confirmData(3)">退回上一步</el-button>
                </el-form-item>
                <el-form-item v-if="this.StepSN > 0" style="height:5px">
                    <el-button type="primary" size="mini" icon="el-icon-check" @click="confirmData(2)">审核</el-button>
                </el-form-item>
                <el-form-item style="height:5px">

                    <el-button type="primary" size="mini" icon="el-icon-tickets" @click="PrintDate()">打印</el-button>
                </el-form-item>
                <el-form-item style="height:5px">
                    <el-button type="primary" size="mini" icon="el-icon-check" @click="handleClose()">关闭</el-button>
                </el-form-item>


            </el-form>

            <span style="
                  text-align: center;
                  font-size: 20px;
                  font-weight: bold;
                  display: inline-block;
                  width: 100%;
                  padding: 3px 0 5px 0;
                ">

                <span>装柜单创建</span>
                <span style="margin-left: 10px">
                    <svg-icon v-if="this.FormData.Status == 0 || this.FormData.Status == null" icon-class="Editing" />
                    <svg-icon v-if="this.FormData.Status == 1" icon-class="ingAudit" />
                    <svg-icon v-if="this.FormData.Status == 2" icon-class="Finish" />
                </span>
            </span>

            <el-form :model="FormData" label-width="90px">
                <Mian-Form ref="refFormData" labelWidth='150' :FormData='FormData' :tableCols='FormCols'
                    :StepSN='StepSN' billIDName='QAcheckupNO' @handleOpen="handleOpen"></Mian-Form>
            </el-form>


            <el-form :inline="true" :model="Search" class="demo-form-inline" size="mini">
            </el-form>


            <el-form :inline="true" :model="Search" class="demo-form-inline" size="mini" style="height:70px"
                v-if="this.StepSN != 0">
            </el-form>
            <el-form ref="form" :model="FormData" :inline="true" :rules="rules" label-position="right"
                label-width="120px">


                <el-button v-if="this.StepSN == 0" type="primary" size="mini" @click="handleOpen('Order')"
                    icon="el-icon-tickets">添加数据</el-button>
                <el-button v-if="this.StepSN == 0" type="primary" size="mini" @click="handleOpen('Order1')"
                    icon="el-icon-tickets">添加数据Vxe</el-button>
                <el-button v-if="this.StepSN == 0" type="primary" size="mini" @click="ADDRow()"
                    icon="el-icon-tickets">添加行</el-button>
                <el-button v-if="this.StepSN == 0" type="primary" size="mini" @click="reinsertion"
                    icon="el-icon-tickets">复插</el-button>




            </el-form>
            <ces-table size='mini' ref="reftableData" :isSelection='false' :isIndex='true' :isSearch='false'
                :isHandle='true' :validRules='validRules' :ToolBarheight="370" :IsfunEdit='false'
                :Isfunfail='this.StepSN == 0 ? true : false' :StepSN=this.StepSN :WFStepSN='WFStepSN'
                :tableData='tableData' :tableCols='tableCols' @handleDelete="delData" @tableCellStyle="tableCellStyle"
                @handrowclick="handrowclick">
            </ces-table>
        </div>


        <el-main style="margin: 1px 10px">
            <span class="blueColor" style="font-size: 15px; font-weight: blod; padding: 10px">审核明细：</span>

            <el-timeline :reverse="true" ref="ListLog" :model="ListLog">
                <el-timeline-item v-for="(activity, index) in ListLog" :key="index"
                    :timestamp="activity.CreateDateTime">
                    {{ activity.Remark }}
                </el-timeline-item>
            </el-timeline>
        </el-main>


        <el-dialog v-if="dialogFormVisible" :visible.sync="dialogFormVisible" :title="人员资料" width="800px">
            <el-form :model="name" label-width="80px" label-position="left">
                <v-AccountOpen :ParmType="ParmType" @FunRowAdd="FunrRowsAdd"></v-AccountOpen>
            </el-form>
        </el-dialog>


        <el-dialog v-if="dialogCUSTVisible" :visible.sync="dialogCUSTVisible" :title="客户资料" width="800px">
            <el-form :model="name" label-width="80px" label-position="left">
                <v-CUSTOpen :ParmType="ParmType" :CUSTtype='1' @FunRowAdd="FunrRowsAdd"></v-CUSTOpen>
            </el-form>
        </el-dialog>


        <el-dialog v-if="dialogPRDTVisible" :visible.sync="dialogPRDTVisible" :title="料品资料" width="800px">
            <el-form :model="name" label-width="80px" label-position="left">
                <v-PRDTOpen :ParmType="ParmType" :CUSTtype='1' @FunRowAdd="FunrRowsAdd"></v-PRDTOpen>
            </el-form>
        </el-dialog>


        <el-dialog v-if="dialogFormOpen" :visible.sync="dialogFormOpen" :title="外发订单资料" width="95%">
            <el-form :model="name" label-width="80px" label-position="left">
                <v-StuffingFormOpen ParmType="Order" :FormDataInterID="FormDataInterID" :SearchData="SearchData"
                    @FunrRowsAdd="FunrRowsAdd"></v-StuffingFormOpen>
            </el-form>
        </el-dialog>

        <el-dialog v-if="dialogFormOpen1" :visible.sync="dialogFormOpen1" :title="外发订单资料" width="95%">
            <el-form :model="name" label-width="80px" label-position="left">
                <vStuffingFormOpenVxe ParmType="Order" :FormDataInterID="FormDataInterID" :SearchData="SearchData"
                    @FunrRowsAdd="FunrRowsAdd"></vStuffingFormOpenVxe>
            </el-form>
        </el-dialog>


    </div>
</template>

<script>


//引入组件
// import cesTable from "../Common//table.vue";
import cesTable from "../Common/ComVxeTable.vue";
import SearchForm from "../Common//Search.vue";
import MianForm from "../Common//form.vue";
import { PrintDate } from "@/api/Print";
import { funDownload, GetTableColsData, GetFormColsData } from "@/api/Common";
//   import { getDataList, UpdateForm, DeleteForm, DeleteRow } from '@/api/MesQMS/QMSSelfReport';
import { getDataList, UpdateForm, DeleteForm, DeleteRow, getfunDownload } from '@/api/Loading/Loading'; //更改

import vGetToolBar from "../Common/GetToolBar";
import vAccountOpen from "../Common/AccountOpen";
import vCUSTOpen from "../Common/CUSTOpen";
import vPRDTOpen from "../Common/PRDTOpen";
import vFormOpen from "../Common/FormOpen";
import vStuffingFormOpen from "./StuffingFormOpen";
import vStuffingFormOpenVxe from "./StuffingFormOpenVxe";
export default {
    name: 'StuffingForm',
    props: {
        inputName: String,
        required: true,
    },

    data() {
        //表单查询一般分为input，下拉框，和日期，下面是下拉框时的设置下拉信息的情况，我的项目没有用到，但是留着方便看

        return {
            dialogFormOpen1: false,
            tableData: [],//表格数据
            FormData: [],//表格数据
            FormCols: [],//表头数据
            tableCols: [],//表格列数据
            StepSN: -1,
            WFStepSN: '0',
            billID: null,
            CompanyAppCode: "",
            ListLog: [],
            IsfunEdit: false,
            Search: {
                // 查询条件
                total: 0, // 总记录数
                pageSize: 13, // 一页显示的记录数
                page: 1, // /当前页码
                RNO: "1623", // 表编号  根据Controller中对应的表编号 //更改
                InterID: "1623",
                jsWhere: "", // 自定义条件
                Download: ""
                // rules: {
                //     UserName: [
                //         { required: true, message: "请输入推荐人称", trigger: "blur" },
                //     ],
                // },

            }, SearchData: {
                // 查询条件
                total: 0, // 总记录数
                pageSize: 13, // 一页显示的记录数
                page: 1, // /当前页码
                RNO: "1595", // 表编号  根据Controller中对应的表编号 //更改
                InterID: "1595",
                jsWhere: " and isnull(Status,0)=2 and NotSandQty>0", // 自定义条件

            },
            SearchWO: {
                // 查询条件
                total: 0, // 总记录数
                pageSize: 0, // 一页显示的记录数
                page: 1, // /当前页码
                RNO: "1622", // 表编号  根据Controller中对应的表编号
                InterID: "1622",
                jsWhere: "", // 自定义条件
                Download: ""
                // Status: 0,
                //   BillState: "BillStateALL",
            },
            cities: [
                {
                    value: "0",
                    label: "草稿",
                },
                {
                    value: "1",
                    label: "审核中",
                },
                {
                    value: "2",
                    label: "结案",
                },
                {
                    value: "3",
                    label: "作废",
                },
            ],
            SearchDialog: false,
            DialogBtnMargin: 0,
            dialogVisible: false,
            dialogFormVisible: false,
            dialogCUSTVisible: false,
            dialogPRDTVisible: false,
            dialogFormOpen: false,
            ParmType: "",
            ParmName: "",
            FormDataInterID: 1622,
            rowIndex: null,
            validRules: [],

        }
    },

    components: { //引入组件后注册组件
        cesTable,
        SearchForm,
        MianForm,
        vAccountOpen,
        vCUSTOpen,
        vPRDTOpen,
        vFormOpen,
        vStuffingFormOpen,
        vStuffingFormOpenVxe
    },
    async mounted() {

        await this.getDataList()

        this.getTableCols()

        // this.getStore()

    },
    async activated() {
        if (this.$route.query.IsPostBack === false) {
            // alert(1)
            await this.getDataList()
            this.getTableCols()
        }
    },
    methods: {
        handrowclick(row) {
            this.rowIndex = row.rowIndex
            // debugger
        },

        async reinsertion() {
            // const result = await UpdateForm({
            //     IsPostBack: false,
            //     InterID: '1623',
            // });
            // let a = result.data1.Table[0];
            let a = JSON.parse(JSON.stringify(this.tableData[this.rowIndex]))
            a.Qty = '0'
            this.tableData.splice(this.rowIndex + 1, 0, a)
            // this.tableCols.prop
            // debugger
        },

        //读取列配置
        async getTableCols() {
            this.tableCols = [];
            this.FormCols = [];
            // debugger
            // const resCols = await getDataList({ data: JSON.stringify(this.SearchWO) });

            const resCols = await GetFormColsData({ InterID: "1622" }); //更改
            this.FormCols = resCols.data;


            // const res = await getDataList({ data: JSON.stringify(this.Search) });
            const res = await GetFormColsData({ InterID: "1623" }); //更改
            this.tableCols = res.data;
            this.validRules = res.validRules;
            // debugger
        },
        async getDataList() {

            // const result = await getDataList({ data: JSON.stringify(this.SearchWO) });


            if (this.inputName == null) {
                this.inputName = this.$route.query.billID;
            }

            const result = await UpdateForm({
                billID: this.inputName,
                IsPostBack: false,
            });
            // this.inputName
            // debugger

            this.FormData = result.data1.Table[0];

            this.tableData = result.data2.Table;

            this.ListLog = result.ListLog;
            this.StepSN = result.StepSN;
            // debugger


        }, OpenFindForm() {
            //打开搜索框
            this.SearchDialog = true;
            //计算外边距
            console.log(this.$refs.searchDialog.offsetHeight);
            this.DialogBtnMargin = this.$refs.searchDialog.offsetHeight - 40;
        }, async confirmData(submit) {
            // 提交数据，需要修改

            const Fresult = true;
            if (submit == 1) {



                let flag = this.$refs['refFormData'].validateForm();



                let flag1 = this.$refs['reftableData'].validateForm();


                if (flag == false || flag1 == false) {
                    this.$notify({
                        title: "不能提交",
                        dangerouslyUseHTMLString: true,
                        message: "不可为空，不能提交",
                        type: "error",
                    });
                    Fresult = false;
                    return false;

                }

                if (this.tableData.length <= 0) {
                    this.$notify({
                        title: "不能提交",
                        dangerouslyUseHTMLString: true,
                        message: "行没有数据，不能提交",
                        type: "error",
                    });
                    Fresult = false;
                    return false;

                }

            }

            let options = "保存中...";

            if (submit == 1) {
                options = "提交中...";
            } else if (submit == 2) {
                options = "审批中...";
            } else if (submit == 3) {
                options = "退回中...";
            }

            const loading = this.$loading({
                lock: true,
                text: options,
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });

            if (Fresult) {
                const result = await UpdateForm({
                    HeadDate: JSON.stringify(this.FormData),
                    BodyDate: JSON.stringify(this.tableData),
                    billID: this.FormData.StuffingNO,
                    IsPostBack: true,
                    submit: submit,
                });
                loading.close();
                this.FormData = result.data1.Table[0];
                this.tableData = result.data2.Table;
                this.ListLog = result.ListLog;
                this.StepSN = result.StepSN;

                if (result.code === 20000) {

                    this.dialogVisible = false;
                    this.$notify({
                        title: result.msg,
                        dangerouslyUseHTMLString: true,
                        message: "",
                        type: "success",
                    });

                    this.$router.push({
                        path: "/Loading/StuffingForm",
                        query: { billID: this.FormData.QMSSelfReportNo, IsPostBack: false },
                    });
                } else {
                    this.$notify({
                        title: result.msg,
                        dangerouslyUseHTMLString: true,
                        message: "",
                        type: "error",
                    });
                }
            }
        },
        handleOpen(name) {


            if (name == "CustomerName") {
                this.ParmType = "CustomerName"; //传入参数
                this.dialogCUSTVisible = true;
            } else if (name == "PMCName") {
                this.ParmType = "PMCName"; //传入参数
                this.dialogFormVisible = true;
            } else if (name == "PrdNo") {
                this.SearchData = {
                    // 查询条件
                    total: 0, // 总记录数
                    pageSize: 13, // 一页显示的记录数
                    page: 1, // /当前页码
                    RNO: "346", // 表编号  根据Controller中对应的表编号 //更改
                    jsWhere: " and isnull(Status,0)=2 and NotIsSendPOQty>0 and BillType=2", // 自定义条件

                }
                this.FormDataInterID = 374;
                this.ParmName = this.FormData.CustomerCode;
                this.ParmType = "PrdNo"; //传入参数
                this.dialogFormOpen = true;
            } else if (name == "Order") {
                this.ParmType = "Order"; //传入参数
                this.dialogFormOpen = true;
            } else if (name == "Order1") {
                this.ParmType = "Order"; //传入参数
                this.dialogFormOpen1 = true;
            }

        },
        async FunrRowsAdd(Rows, ParmType) {
            //添加人员
            // Rows 
            // ParmType
            // debugger
            // this.multipleTable = Rows[0];

            if (ParmType == "QCName") {
                this.FormData.QCCode = this.multipleTable.UserCode;
                this.FormData.QCName = this.multipleTable.UserName;
                this.dialogFormVisible = false;
            } else if (ParmType == "PMCName") {
                this.FormData.PMCCode = this.multipleTable.UserCode;
                this.FormData.PMCName = this.multipleTable.UserName;
                this.dialogFormVisible = false;
            } else if (ParmType == "CustomerName") {
                this.FormData.CustomerCode = this.multipleTable.CUS_NO;
                this.FormData.CustomerName = this.multipleTable.NAME;
                this.dialogCUSTVisible = false;
            } else if (ParmType == "Order") {

                for (let v of Rows) {

                    //判断重复不能添加
                    let Isjudge = this.tableData.filter(function (item) {
                        if (item.SourceID == v.ID) {
                            return item;
                        }
                    })
                    if (Isjudge.length == 0) {
                        this.multipleTable = v;
                        let row = {
                            PrdNo: this.multipleTable.ProductNo,
                            PrdName: this.multipleTable.Name,
                            SPC: this.multipleTable.ProductDesc,
                            // UT: this.multipleTable.UT,
                            // SourceNo: this.multipleTable.POrderNo,
                            SourceID: this.multipleTable.ID,
                            Qty: this.multipleTable.PlannedShipQty,

                        };
                        this.tableData.push(row);
                        // this.FormData.CustomerCode = this.multipleTable.CustomerCode;
                    }
                }

                //过滤重复的
                // let data = Rows.filter(v => {
                //     let a = this.tableData.some(x => {
                //         return v.ID == x.ID
                //     })
                //     // a
                //     // this.tableData
                //     // debugger
                //     return !a

                // })
                // // debugger
                // // 数据结构
                // data.forEach(row => {
                //     let data = {}
                //     this.tableCols.forEach(v =>{
                //         data[v.prop] = row[v.prop]
                //     })
                //     data.ID = ''
                //     this.tableData.push(data)
                //     // this.tableData
                //     // data
                //     // Rows
                //     // debugger
                // })
                this.dialogFormOpen1 = false;
                this.dialogFormOpen = false;
            }
            //  else if (ParmType == "Order") {


            //     for (let v of Rows) {

            //         //判断重复不能添加
            //         let Isjudge = this.tableData.filter(function (item) {
            //             if (item.SourceID == v.ID) {
            //                 return item;
            //             }
            //         })

            //         this.multipleTable = v;
            //         this.FormData.PrdNo = this.multipleTable.PrdNo;
            //         this.FormData.PrdName = this.multipleTable.PrdName;
            //         this.FormData.SourceNo = this.multipleTable.POrderNo;
            //         this.FormData.SPC = this.multipleTable.SPC;
            //         this.FormData.SourceID = this.multipleTable.ID;
            //         this.FormData.OrderNo = this.multipleTable.POrderNo;
            //         this.FormData.CustomerCode = this.multipleTable.CustomerCodes;
            //         this.FormData.CustomerName = this.multipleTable.CustomerNames;
            //         this.FormData.Qty = this.multipleTable.Qty;
            //         this.FormData.ProductModel = this.multipleTable.ProductModel;
            //         this.FormData.SaleTo = this.multipleTable.Extend4;
            //         this.FormData.IDX2Name = this.multipleTable.IDX2Name;




            //         //  if(Isjudge.length==0)
            //         //  {
            //         //       this.multipleTable = v;
            //         //       let row = {
            //         //         PrdNo: this.multipleTable.PrdNo,
            //         //         PrdName: this.multipleTable.PrdName,
            //         //         SPC: this.multipleTable.SPC,
            //         //         UT: this.multipleTable.UT,
            //         //         SourceNo: this.multipleTable.PUOrderNo,
            //         //         SourceID: this.multipleTable.ID,
            //         //         Qty: this.multipleTable.NotSandQty,

            //         //       };
            //         //       this.tableData.push(row);

            //         //  }

            //     }

            //     this.dialogFormOpen = false;
            //     this.dialogFormOpen1 = false;
            // }
            else if (ParmType == 'PRD') {

                for (let v of Rows) {

                    //判断重复不能添加
                    let Isjudge = this.tableData.filter(function (item) {
                        if (item.PrdNo == v.PrdNo) {
                            return item;
                        }
                    })





                    if (Isjudge.length == 0) {
                        this.multipleTable = v;
                        let row = {
                            PrdNo: this.multipleTable.PrdNo,
                            PrdName: this.multipleTable.PrdName,
                            SPC: this.multipleTable.SPC,
                            UT: this.multipleTable.UT,
                            Extend1: this.multipleTable.ProducingArea,
                            Extend2: this.multipleTable.BatchNumber,
                            Extend3: this.multipleTable.PackMaterial,
                            Qty: null,
                            Weight: null,

                        };
                        this.tableData.push(row);

                    }

                }

                this.dialogPRDTVisible = false;

            }



        },
        async ADDRow() {
            // let row = {
            //   SorNo: null,
            //   QAcheckupNO: this.FormData.QAcheckupNO,
            //   TermType: null,
            //   Term: null,
            //   Qcresult: OK,
            //   JudgmentGrade: null,
            //   ID: null,
            // };
            const result = await UpdateForm({
                IsPostBack: false,
                InterID: '1621',
            });
            let a = result.data1.Table[0];
            // this.dialogVisible = true;
            // debugger
            this.tableData.push(a);
        },
        handleClose() {
            this.$store.dispatch('tagsView/delView', this.$route)
            // 返回上一步路由
            this.$router.go(-1)
        }, delData({ rowIndex, row }) {
            this.$confirm('删除操作, 是否继续?', '提示', {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            })
                .then(async () => {


                    this.tableData.splice(rowIndex, 1);



                    if (row.ID == null) { //更改
                        return false;
                    }

                    const result = await DeleteRow({
                        ID: row.ID,
                        billID: this.inputName,
                        types: 1,
                        IsPostBack: false,
                    });

                    this.$message({
                        type: "success",
                        message: result.msg,
                    });

                })
                .catch((err) => {
                    console.error(err);
                });
        }, async PrintDate() {
            // 提交数据，需要修改

            if (this.CompanyAppCode == "04") {

                // await PrintDate('T20220704001',this.FormData,this.tableData,2);

            } else {
                //await PrintDate('T20210721004',this.FormData,this.tableData,2);
            }







        }, tableCellStyle(e, callback) {

            // debugger
            if (e.row.QAresult == "NG") {
                callback({ backgroundColor: "#ffc1cc" })
            }
            else {
                callback({})
            }

        }

    }
}
</script>

<style lang="scss" scoped>
.app-container {
    /deep/ .el-dialog {
        margin-top: 5px !important
    }

    /deep/ .el-dialog__body {
        padding: 20px !important;
    }

    .EditObjs-table {
        margin-top: 30px;
    }

    .permission-tree {
        margin-bottom: 5px;
    }
}

.searchDialog ::v-deep .el-dialog__header {
    background: #409eff;
}

.searchDialog ::v-deep .el-dialog__title {
    color: #fff;
}

.searchDialog ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
}

.infoColor {
    font-weight: bold;
    color: #909399;
}

.redColor {
    font-weight: bold;
    color: red;
}

.blueColor {
    font-weight: bold;
    color: #409eff;
}

.orangeColor {
    font-weight: bold;
    color: orange;
}

.greenColor {
    font-weight: bold;
    color: #67c23a;
}
</style>